﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Stee.CAP8.MessageBus.Sagas.Core.Messages
{
    public class MessageHeader
    {
        #region 消息发起方
        public string SourceTopicName { get; set; }
        public string SourceClassName { get; set; }
        public string SourceActionName { get; set; }
        #endregion

        #region 消息接收方
        public string DestinationTopicName { get; set; }
        public string DestinationClassName { get; set; }
        public string DestinationActionName { get; set; } 
        #endregion

        public bool IsReply { get; set; }

        public void SetHeaders(Func<string, object,bool> callback)
        {
            callback(nameof(SourceTopicName), SourceTopicName);
            callback(nameof(SourceClassName), SourceClassName);
            callback(nameof(SourceActionName), SourceActionName);

            callback(nameof(DestinationTopicName), DestinationTopicName);
            callback(nameof(DestinationClassName), DestinationClassName);
            callback(nameof(DestinationActionName), DestinationActionName);

            callback(nameof(IsReply), IsReply);
        }
        public Dictionary<string, object> ToDictionary()
        {

            var dictionary = new Dictionary<string, object>
            {
                [nameof(SourceTopicName)] = SourceTopicName,
                [nameof(SourceActionName)] = SourceActionName,
                [nameof(DestinationTopicName)] = DestinationTopicName,
                [nameof(DestinationActionName)] = DestinationActionName,
                [nameof(IsReply)] = IsReply
            };
            return dictionary;
        }

        public MessageHeader(Dictionary<string, object> keyValuePairs)
        {
            if (keyValuePairs is null)
            {
                throw new ArgumentNullException(nameof(keyValuePairs));
            }
            SourceTopicName = keyValuePairs[nameof(SourceTopicName)].ToString();
            SourceActionName = keyValuePairs[nameof(SourceActionName)].ToString();
            SourceClassName = keyValuePairs[nameof(SourceClassName)].ToString();

            DestinationTopicName = keyValuePairs[nameof(DestinationTopicName)].ToString();
            DestinationActionName = keyValuePairs[nameof(DestinationActionName)].ToString();
            DestinationClassName = keyValuePairs[nameof(DestinationClassName)].ToString();
            IsReply = Convert.ToBoolean(keyValuePairs[nameof(IsReply)]);
           
        }

        public MessageHeader()
        {

        }

      


    }
}
